
#include "main.h"

manual_operation::manual_operation(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::manual_operation)
{
    ui->setupUi(this);
     QObject::connect(ui->return_btn,SIGNAL(clicked()),this,SLOT(back_clicked()));
     auto_keep_temperature=0;
     ui->pressure_display->setpressure_display();
     timer=new QTimer();
     QObject:: connect( timer, SIGNAL(timeout()), SLOT(timeDone()) );
     exhaust_timer=new QTimer();
     QObject:: connect( exhaust_timer, SIGNAL(timeout()), SLOT(exhaust_timeDone()) );
}

manual_operation::~manual_operation()
{
    delete ui;
    delete timer;
}

void manual_operation::entrance()
{
      timer->start(500);
      showFullScreen();
      //sterilizing=1;
      //temperature_keep=1;

      if( whole_machine_parameter.bottom_exhaust_machine)
      {

         ui->air_back_btn->hide();
         ui->pump_btn->hide();

      }
      else {
          ui->air_back_btn->show();
          ui->pump_btn->show();

      }
      ui->timer_dial->hide();
      ui->label_timer->hide();
      ui->lcd_timer->hide();
}

void manual_operation::exhaust_timeDone()
{
    exhaust= true;
}


void manual_operation::timeDone()
{
    if(door_closed_OK)
   {
     ui->door_operation_btn->setText(trUtf8("门关闭"));
     ui->door_operation_btn->SET_COLOR_GREEN
    }
    else
     {
       ui->door_operation_btn->setText(trUtf8("门未关"));
       ui->door_operation_btn->SET_COLOR_RED
//               air_intake_valve_OFF
//               exhaust_valve_OFF
//               air_supply_valve_OFF
        }



    if( auto_keep_temperature )
      {

        if(TEM > keep_temperature) { heating_valve_OFF }
        else { if(TEM < keep_temperature - 0.2) heating_valve_ON }
        if( constant_temperature_time>0 )
        {
            if( ++count==120) {
            count=0;
            ui->timer_dial->setValue(--constant_temperature_time);
            ui->lcd_timer->display(constant_temperature_time);
              }
         }
        else{
              ui->keepTemperature_btn->click();
            }


        if( exhaust) { exhaust_valve_ON  exhaust=false;}
        else exhaust_valve_OFF
        }


    if(heating_valve_isopen)
   {
   ui->heat_btn->setText(trUtf8("正在加热"));
   ui->heat_btn->SET_COLOR_GREEN
    }
    else
    {
    ui->heat_btn->setText(trUtf8("加热"));
    ui->heat_btn->SET_COLOR_GRAY
    }

}

void manual_operation::back_clicked()
{
     BUTTON_SOUND;
     STOP_ACTION
     ui->keepTemperature_btn->SET_COLOR_GRAY
     ui->air_back_btn->setText(trUtf8("回气"));
     ui->air_back_btn->SET_COLOR_GRAY
     ui->intake_btn->setText(trUtf8("进气"));
     ui->intake_btn->SET_COLOR_GRAY
     ui->exhaust_btn->setText(trUtf8("排气"));
     ui->exhaust_btn->SET_COLOR_GRAY
     ui->pump_btn->setText(trUtf8("抽气"));
     ui->pump_btn->SET_COLOR_GRAY
     auto_keep_temperature=0;
     sterilizing=0;
     sterilization_protype= NO_CHOICE;
     timer->stop();
     this->close();
}

void manual_operation::on_pump_btn_clicked()
{       BUTTON_SOUND;
 {
    if(ui->pump_btn->isChecked())
   {

        if(ui->exhaust_btn->isChecked())
        {
          ui->exhaust_btn->setChecked(false);
          on_exhaust_btn_clicked();
          exhaust_valve_OFF;
        }

    Vacuum_pump_ON;
   ui->pump_btn->setText(trUtf8("正在抽气"));
   ui->pump_btn->SET_COLOR_GREEN

    }
    else
    {
    Vacuum_pump_OFF;
    ui->pump_btn->setText(trUtf8("抽气"));
    ui->pump_btn->SET_COLOR_GRAY
    }
 }
}



void manual_operation::on_intake_btn_clicked()
{
       BUTTON_SOUND;
   if(door_closed_OK)
 {
    if(ui->intake_btn->isChecked())
   {

        if(ui->air_back_btn->isChecked())
        {
          ui->air_back_btn->setChecked(false);
          on_air_back_btn_clicked();
          air_supply_valve_OFF;
        }
   ui->intake_btn->setText(trUtf8("正在进气"));
   ui->intake_btn->SET_COLOR_GREEN
   air_intake_valve_ON;
    }
    else
    {
    air_intake_valve_OFF;
    ui->intake_btn->setText(trUtf8("进气"));
    ui->intake_btn->SET_COLOR_GRAY
    }
 }
}

void manual_operation::on_exhaust_btn_clicked()
{    BUTTON_SOUND;

    if(ui->exhaust_btn->isChecked())
   {
        if(ui->pump_btn->isChecked())
        {
          ui->pump_btn->setChecked(false);
          on_pump_btn_clicked();
          Vacuum_pump_OFF;
        }
        if(ui->air_back_btn->isChecked())
        {
          ui->air_back_btn->setChecked(false);
          on_air_back_btn_clicked();
          air_supply_valve_OFF;
        }

        ui->exhaust_btn->setText(trUtf8("正在排气"));
        ui->exhaust_btn->SET_COLOR_GREEN
         exhaust_valve_ON;
    }
    else
        {
            exhaust_valve_OFF;
            ui->exhaust_btn->setText(trUtf8("排气"));
            ui->exhaust_btn->SET_COLOR_GRAY
        }

}

void manual_operation::on_air_back_btn_clicked()
{    BUTTON_SOUND;
 {
    if(ui->air_back_btn->isChecked())
   {

        if(ui->intake_btn->isChecked())
        {
          ui->intake_btn->setChecked(false);
          on_intake_btn_clicked();
           air_intake_valve_OFF;
        }

        if(ui->exhaust_btn->isChecked())
        {
          ui->exhaust_btn->setChecked(false);
          on_exhaust_btn_clicked();
          exhaust_valve_OFF;
        }

   ui->air_back_btn->setText(trUtf8("正在回气"));
   ui->air_back_btn->SET_COLOR_GREEN
   air_supply_valve_ON;
    }
    else
    {

    air_supply_valve_OFF;
    ui->air_back_btn->setText(trUtf8("回气"));
    ui->air_back_btn->SET_COLOR_GRAY
    }
 }
}



void manual_operation::on_heat_btn_clicked()
{
    BUTTON_SOUND;
    if(ui->heat_btn->isChecked())
   {
  // ui->heat_btn->setText(trUtf8("正在加热"));
  // ui->heat_btn->SET_COLOR_GREEN
   heating_valve_ON;
    }
    else
    {
    heating_valve_OFF;
  //  ui->heat_btn->setText(trUtf8("加热"));
  //  ui->heat_btn->SET_COLOR_GRAY
    }
}

void manual_operation::on_keepTemperature_btn_clicked()
{
    BUTTON_SOUND;
     if(workable)
     {
   if( ui->keepTemperature_btn->isChecked())
  {     
    ui->timer_dial->show();
    ui->label_timer->show();
    ui->lcd_timer->show();
    ui->keepTemperature_btn->SET_COLOR_GREEN
    auto_keep_temperature=1;
     count=0;
    keep_temperature=TEM;
   constant_temperature_time=10;
   ui->timer_dial->setValue(constant_temperature_time);
   ui->lcd_timer->display(constant_temperature_time);
    ui->keepTemperature_btn->setText(trUtf8("恒温\n" )+ QString::number(TEM,'f',1));
    //temperature_keep=1;
     air_intake_valve_ON
     ui->heat_btn->setEnabled(false);
     ui->intake_btn->setEnabled(false);
     ui->exhaust_btn->setEnabled(false);
     ui->air_back_btn->setEnabled(false);
     ui->pump_btn->setEnabled(false);
   }
   else
   {
   // temperature_keep=0;
    auto_keep_temperature=0;
    ui->keepTemperature_btn->setText(trUtf8("恒温"));
    ui->keepTemperature_btn->SET_COLOR_GRAY
    ui->timer_dial->hide();
    ui->label_timer->hide();
     ui->lcd_timer->hide();
    ui->heat_btn->setEnabled(true);
    ui->intake_btn->setEnabled(true);
    ui->exhaust_btn->setEnabled(true);
    ui->air_back_btn->setEnabled(true);
    ui->pump_btn->setEnabled(true);
   }
    }
}

void manual_operation::on_door_operation_btn_clicked()
{
      BUTTON_SOUND;
      door_operation->enter();
}


 void manual_operation::on_timer_dial_sliderMoved(int position)
 {
   // constant_temperature_time=ui->timer_dial->value();
    constant_temperature_time=position;
    ui->lcd_timer->display(constant_temperature_time);
 }
